package StudyCode.summary.zipfile;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Path;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class ArchivesListTargetFilesZIPArchive {
	protected static final int bufferSize = 1024;


	public static void archive(Path archive, List<Path> targetFiles)
			throws Exception {
		byte[] buffer = new byte[bufferSize];
		try (ZipOutputStream zos = new ZipOutputStream(
				new FileOutputStream(archive.toFile()))) {

			for (Path file : targetFiles) {
				ZipEntry ze = new ZipEntry(file.getFileName().toString());
				zos.putNextEntry(ze);
				try (FileInputStream in = new FileInputStream(file.toFile())) {
					int len;
					while ((len = in.read(buffer)) > 0) {
						zos.write(buffer, 0, len);
					}
				} catch (IOException ioe_inner) {
					ioe_inner.printStackTrace();
				}
				zos.closeEntry();
			}
		} catch (IOException ioe_outter) {
			ioe_outter.printStackTrace();
		}
	}
}
